package cn.glc.daily.life.web.service.dict.impl;

import cn.glc.daily.life.common.exceptions.GlobalException;
import cn.glc.daily.life.infrastructure.po.DictPo;
import cn.glc.daily.life.infrastructure.repository.mysql.MysqlRepositories;
import cn.glc.daily.life.model.dto.dict.DictDeleteDto;
import cn.glc.daily.life.model.dto.dict.DictDto;
import cn.glc.daily.life.model.dto.dict.DictOpenOrCloseDto;
import cn.glc.daily.life.model.dto.dict.DictSaveOrUpdateDto;
import cn.glc.daily.life.model.vo.dict.DictVo;
import cn.glc.daily.life.model.vo.page.PageConvert;
import cn.glc.daily.life.web.convert.DictConvert;
import cn.glc.daily.life.web.service.dict.DictService;
import cn.simon.free.tools.utils.DateUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static cn.glc.daily.life.common.enums.GlobalCodeEnum.DictBusinessExceptionEnum.DICT_CANT_NOT_DELETE;

/**
 * @author simon
 * @date 2021/4/13 12:00
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class DictServiceImpl implements DictService {

    @Override
    public IPage<DictVo> listPage(DictDto dictDto) {
        return MysqlRepositories.dictRepository.listPage(PageConvert.convert2PageQuery(dictDto), dictDto)
                .convert(DictConvert.INSTANCE::dictPoToDictVo);
    }

    @Override
    public List<DictVo> queryChildrenByCondition(DictDto dictDto) {
        List<DictPo> DictPoList = MysqlRepositories.dictRepository.queryChildrenByCondition(dictDto);
        return DictConvert.INSTANCE.dictPoToDictVo(DictPoList);
    }

    @Override
    public DictVo queryByCondition(DictDto dictDto) {
        DictPo dictPo = MysqlRepositories.dictRepository.queryByCondition(dictDto);
        return DictConvert.INSTANCE.dictPoToDictVo(dictPo);
    }

    @Override
    public Boolean save(DictSaveOrUpdateDto dictSaveOrUpdateDto) {
        DictPo newDictPo = DictConvert.INSTANCE.normalConvert(dictSaveOrUpdateDto);
        DictPo DictPo = MysqlRepositories.dictRepository.findByCode(newDictPo.getCode());
        if (Objects.isNull(DictPo)) {
            newDictPo.setCode("0".equals(newDictPo.getParentCode()) ? newDictPo.getCode() : newDictPo.getParentCode().concat("_").concat(newDictPo.getCode()));
//            newDictPo.setCreator(UaaAuthHelper.getOperator().getName());
            newDictPo.setUpdateTime(DateUtils.now());
            MysqlRepositories.dictRepository.save(newDictPo);
            return true;
        }
        return false;
    }

    @Override
    public Boolean update(DictSaveOrUpdateDto dictSaveOrUpdateDto) {
        DictPo newDictPo = DictConvert.INSTANCE.normalConvert(dictSaveOrUpdateDto);
        DictPo DictPo = MysqlRepositories.dictRepository.findByCode(newDictPo.getCode());
        if (Objects.nonNull(DictPo)) {
            DictPo.setTitle(newDictPo.getTitle());
            DictPo.setDictValue(newDictPo.getDictValue());
            DictPo.setSequence(newDictPo.getSequence());
            DictPo.setRemark(newDictPo.getRemark());
            DictPo.setWeight(newDictPo.getWeight());
            DictPo.setIsOpen(newDictPo.getIsOpen());
            // TODO: 2021/4/13 修改人
            DictPo.setModifier("");
            DictPo.setUpdateTime(DateUtils.now());
            MysqlRepositories.dictRepository.updateById(DictPo);
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(DictDeleteDto dictDeleteDto) {
        DictPo DictPo = MysqlRepositories.dictRepository.findByCode(dictDeleteDto.getCode());
        if (Objects.nonNull(DictPo) && dictDeleteDto.getIsDelete()) {
            Integer count = MysqlRepositories.dictRepository.queryChildrenCountByCondition(dictDeleteDto.getCode());
            //没有子节点信息才能删除当前节点的数据
            Optional.of(count).filter(c -> c == 0).orElseThrow(() -> new GlobalException(DICT_CANT_NOT_DELETE));
            MysqlRepositories.dictRepository.removeById(DictPo.getId());
            return true;
        }
        return false;
    }

    @Override
    public Boolean openOrClose(DictOpenOrCloseDto dictOpenOrCloseDto) {
        DictPo newDictPo = DictConvert.INSTANCE.normalConvert(dictOpenOrCloseDto);
        DictPo DictPo = MysqlRepositories.dictRepository.findByCode(newDictPo.getCode());
        if (Objects.nonNull(DictPo)) {
            DictPo.setIsOpen(newDictPo.getIsOpen());
            // TODO: 2021/4/13 修改人
            DictPo.setModifier("");
            DictPo.setUpdateTime(DateUtils.now());
            MysqlRepositories.dictRepository.updateById(DictPo);
            return true;
        }
        return true;
    }

}
